package com.example.lotterysystem.service.mq;

import cn.hutool.db.handler.StringHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.example.lotterysystem.common.config.DirectRabbitConfig.*;

@Component
@RabbitListener(queues = DLX_QUEUE_NAME)
public class DlxReceiver {
    private static final Logger logger = LoggerFactory.getLogger(DlxReceiver.class);
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //处理死信队列,进行消息重发
    @RabbitHandler
    public void process(Map<String,String> message){
        //处理方法
        logger.info("开始处理异常消息!");
        rabbitTemplate.convertAndSend(EXCHANGE_NAME,ROUTING,message);

        //用来演示处理异常的流程
        //正确流程:(根据目的来设计)[扩展]
        //1.接收到异常消息之后,将消息存放到数据库表中(进行持久化存储)
        //2.存放后当前异常消息消费完成,死信队列消息处理完成,但异常消息被持久化存储到表中
        //3.解决异常
        //4.完成脚本任务,判断异常消息表中是否有数据(有就代表还有消息未完成)
        //5.处理消息:将消息发送给普通队列
    }
}
